using Microsoft.AspNetCore.Mvc;
using JTZY.Model.Agent.Dto;
using JTZY.Model.Agent;
using JTZY.Service.Agent.IAgentService;
using JTZY.Admin.WebApi.Filters;
using JTZY.Service.Opdata;
using Aliyun.OSS;
using JTZY.Model.Stock.Dto;

//创建时间：2025-02-04
namespace JTZY.Admin.WebApi.Controllers.Agent
{
    /// <summary>
    /// 代理商订单记录表
    /// </summary>
    [Verify]
    [Route("agent/Corecmsagentorder")]
    public class CorecmsagentorderController : BaseController
    {
        /// <summary>
        /// 代理商订单记录表接口
        /// </summary>
        private readonly ICorecmsagentorderService _CorecmsagentorderService;
        private readonly ISaleDataService _saleDataService;

        public CorecmsagentorderController(ICorecmsagentorderService CorecmsagentorderService, ISaleDataService saleDataService)
        {
            _CorecmsagentorderService = CorecmsagentorderService;
            _saleDataService = saleDataService;
        }

        /// <summary>
        /// 查询代理商订单记录表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("list")]
        [ActionPermissionFilter(Permission = "corecmsagentorder:list")]
        public IActionResult QueryCorecmsagentorder([FromQuery] CorecmsagentorderQueryDto parm)
        {
            parm.UserId = HttpContext.GetUId();
            var response = _CorecmsagentorderService.GetList(parm);
            return SUCCESS(response);
        }


        /// <summary>
        /// 查询代理商订单记录表详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("{Id}")]
        [ActionPermissionFilter(Permission = "corecmsagentorder:query")]
        public IActionResult GetCorecmsagentorder(int Id)
        {
            var response = _CorecmsagentorderService.GetInfo(Id);
            
            var info = response.Adapt<CorecmsagentorderDto>();
            return SUCCESS(info);
        }

        /// <summary>
        /// 添加代理商订单记录表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "corecmsagentorder:add")]
        [Log(Title = "代理商订单记录表", BusinessType = BusinessType.INSERT)]
        public IActionResult AddCorecmsagentorder([FromBody] CorecmsagentorderDto parm)
        {
            var modal = parm.Adapt<Corecmsagentorder>().ToCreate(HttpContext);

            var response = _CorecmsagentorderService.AddCorecmsagentorder(modal);

            return SUCCESS(response);
        }

        /// <summary>
        /// 更新代理商订单记录表
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "corecmsagentorder:edit")]
        [Log(Title = "代理商订单记录表", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdateCorecmsagentorder([FromBody] CorecmsagentorderDto parm)
        {
            var modal = parm.Adapt<Corecmsagentorder>().ToUpdate(HttpContext);
            var response = _CorecmsagentorderService.UpdateCorecmsagentorder(modal);

            return ToResponse(response);
        }

        /// <summary>
        /// 删除代理商订单记录表
        /// </summary>
        /// <returns></returns>
        [HttpPost("delete/{ids}")]
        [ActionPermissionFilter(Permission = "corecmsagentorder:delete")]
        [Log(Title = "代理商订单记录表", BusinessType = BusinessType.DELETE)]
        public IActionResult DeleteCorecmsagentorder([FromRoute]string ids)
        {
            var idArr = Tools.SplitAndConvert<int>(ids);

            return ToResponse(_CorecmsagentorderService.Delete(idArr));
        }

        [HttpGet("querysaledata")]
        [ActionPermissionFilter(Permission = "corecmsagentorder:list")]
        public IActionResult QuerySaleData([FromQuery] CorecmsagentorderQueryDto parm)
        {
            parm.UserId = HttpContext.GetUId();
            var response = _saleDataService.GetSaleData(parm);
            response.ForEach(item => { 
                //退货率计算
                item.retRate = item.retNums > 0 ? ((item.retNums*1.0 / item.Nums)).ToString("0.00%") : "0%";
            });
            return SUCCESS(response);
        }

        [HttpGet("queryallsaledata")]
        [ActionPermissionFilter(Permission = "corecmsagentorder:list")]
        public IActionResult QueryAllUserSalesDataForLatestYear(int timeType) {
            var userId = HttpContext.GetUId();
            var response = _saleDataService.GetSaleSummaryByTimeType(timeType,userId);
            return SUCCESS(response);

        }

        [HttpGet("queryitemsaledata")]
        [ActionPermissionFilter(Permission = "corecmsagentorder:list")]
        public IActionResult QueryItemTypeSalesData(int timeType)
        {
            var userId = HttpContext.GetUId();
            var response = _saleDataService.GetSaleSummaryByItemType(userId);
            return SUCCESS(response);
        }

        [HttpGet("queryrptsaledetaildata")]
        [ActionPermissionFilter(Permission = "corecmsagentorder:list")]
        public IActionResult GetRptSaleDetail(RptSaleDetailQueryDto request) {
            var userId = HttpContext.GetUId();
            request.agentId = userId;
            var response = _saleDataService.GetRptSaleDetailPageList(request);
            return SUCCESS(response);

        }

        [HttpGet("queryrptsalesummarydata")]
        [ActionPermissionFilter(Permission = "corecmsagentorder:list")]
        public IActionResult GetRptSaleSummary(RptSaleSummaryQueryDto request)
        {
            var userId = HttpContext.GetUId();
            request.agentId = userId;
            var response = _saleDataService.GetRptSaleSummaryPageList(request);
            return SUCCESS(response);

        }

        /// <summary>
        /// 导出销售统计
        /// </summary>
        /// <returns></returns>
        [Log(Title = "销售统计", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)]
        [HttpGet("export")]
        public IActionResult Export([FromQuery] CorecmsagentorderQueryDto parm)
        {
            parm.UserId = HttpContext.GetUId();
            var response = _saleDataService.GetSaleData(parm);
            response.ForEach(item => {
                //退货率计算
                item.retRate = item.retNums > 0 ? ((item.retNums * 1.0 / item.Nums)).ToString("0.00%") : "0%";
            });
            if (response == null || response.Count <= 0)
            {
                return ToResponse(ResultCode.FAIL, "没有要导出的数据");
            }
            var result = ExportExcelMini(response, "销售统计", "销售统计");
            return ExportExcel(result.Item2, result.Item1);
        }
    }
}